define([
  'text!templates/common/showModal.html',
], function (ModalTemplate) {

    var view = Backbone.View.extend({
        el: $("#main-content"),
        render: function (options) {

            this.$el.find("#main-modal").remove();


            var template = Handlebars.compile(ModalTemplate);
            var html = template(options);
            this.$el.append(html);

            if (options.hasOwnProperty("html")) {
                $("#main-modal .modal-body").html(options.html);
            }

            if (options.hasOwnProperty("backdrop")) {
                $("#main-modal").modal({
                    "backdrop": options.backdrop
                })
            }

            $("#main-modal .modal-footer button").click(function () {

                var res = true;
                if (options.callback) {
                    if (options.callback.call(this, $(this).attr("data-code"), $("#main-modal")) === false)
                        res = false;
                }

                if (res) {
                    $("#main-modal").off('shown');
                    $("#main-modal").modal("hide");
                }

                return false;
            });

            $("#main-modal").on('shown', function () {
                $("#main-modal button.btn-primary").focus();
            });
            $("#main-modal").on('hidden', function () {
                $("#main-modal").off('hidden');
                if (options.hasOwnProperty("onClose")) {
                    options.onClose($("#main-modal"));
                }
                $("#main-modal").remove();
            })

            $("#main-modal").modal("show");

            if (options.hasOwnProperty("onLoad")) {
                options.onLoad($("#main-modal"));
            }


        },

    });
    return view;
});